package com.m3839.sdk.pay.ui;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.m3839.sdk.common.base.BaseDialogFragment;
import com.m3839.sdk.common.util.ClipboardUtils;
import com.m3839.sdk.common.util.DateUtils;
import com.m3839.sdk.common.util.ResourcesUtils;
import com.m3839.sdk.common.util.SpanUtils;
import com.m3839.sdk.common.util.ToastUtil;
import com.m3839.sdk.pay.bean.PayConfig;
import com.m3839.sdk.pay.bean.PayResult;
import com.m3839.sdk.pay.constant.PayConstant;
import com.m3839.sdk.pay.task.PayTask;

/**
 * Description:该对话框是用来显示支付的结果
 *
 * @author 陈宝阳
 * @create 2020/8/6 10:10
 */
public class PayResultDialogFragment extends BaseDialogFragment {

  private static final String HYKB_QQ_COPY_TIP = "已复制QQ号";

  private ImageView payStatusIv;
  private TextView payStatusTv;

  private TextView retryBtn;
  private TextView problemTv;

  private PayResult payResult;
  private PayConfig payConfig;

  private PayTask payTask;

  public static PayResultDialogFragment getInstance(PayConfig config,
                                                    PayResult result) {
    PayResultDialogFragment fragment = new PayResultDialogFragment();
    Bundle data = new Bundle();
    data.putParcelable(PayConstant.HYKB_KEY_PAY_RESULT, result);
    data.putParcelable(PayConstant.HYKB_KEY_PAY_CONFIG, config);
    fragment.setArguments(data);
    return fragment;
  }

  public void update(PayConfig config, PayResult result) {
    payConfig = config;
    payResult = result;
    setContent();
    initListener();
  }

  public void setPayTask(PayTask payTask) {
    this.payTask = payTask;
  }

  @Override
  protected void parseArgs() {
    payConfig = getArguments().getParcelable(PayConstant.HYKB_KEY_PAY_CONFIG);
    payResult = getArguments().getParcelable(PayConstant.HYKB_KEY_PAY_RESULT);
  }

  @Override
  protected String getLayoutName() {
    return "hykb_pay_result_dialog_fragment";
  }

  @Override
  protected void initView(View view) {
    payStatusIv = viewHolder.findView("iv_pay_status");
    payStatusTv = viewHolder.findView("tv_pay_status");

    retryBtn = viewHolder.findView("tv_retry_btn");
    problemTv = viewHolder.findView("tv_contact_problem");
  }

  @Override
  protected void setContent() {
    payStatusTv.getPaint().setFakeBoldText(true);
    String timeOrReasonTitle;
    String timeOrReason;
    if (payResult.isSuccess()) {
      payStatusIv.setImageResource(
          ResourcesUtils.getDrawableId(getActivity(), "hykb_img_pay_success"));
      payStatusTv.setTextColor(Color.parseColor(PayConstant.HYKB_COLOR_GREEN));
      payStatusTv.setText(PayConstant.HYKB_STRING_PAY_SUCCESS);
      //支付成功显示支付时间
      timeOrReasonTitle = PayConstant.HYKB_STRING_PAY_TIME;
      timeOrReason = DateUtils.getCurrentTime();
      retryBtn.setVisibility(View.GONE);
    } else {
      //支付失败，判断是否需要显示重试按钮
      if (isShowRetry()) {
        retryBtn.setVisibility(View.VISIBLE);
      } else {
        retryBtn.setVisibility(View.GONE);
      }
      payStatusIv.setImageResource(
          ResourcesUtils.getDrawableId(getActivity(), "hykb_img_pay_fail"));
      payStatusTv.setTextColor(Color.parseColor(PayConstant.HYKB_COLOR_DEEP_DARK));
      payStatusTv.setText(PayConstant.HYKB_STRING_PAY_FAILED);
      //支付失败显示失败原因
      timeOrReasonTitle = PayConstant.HYKB_STRING_PAY_FAILED_REASON;
      timeOrReason = payResult.message;

    }

    // 设置支付支付时间，或者支付失败原因
    viewHolder.setTextView("tv_time_reason_title", timeOrReasonTitle);
    viewHolder.setTextView("tv_time_or_reason", timeOrReason);

    //设置订单ID，商品名称，充值金额文案
    viewHolder.setTextView("tv_order_id",payResult.payInfo.cpOrderId);
    viewHolder.setTextView("tv_goods_name", payResult.payInfo.goodsName);
    viewHolder.setTextView("tv_pay_money",
            String.format(PayConstant.HYKB_STRING_PAY_MONEY_FORMAT, payResult.payInfo.money));

    problemTv.setTextColor(Color.parseColor(PayConstant.HYKB_COLOR_BLUE));
    problemTv.setText(PayConstant.HYKB_STRING_ORDER_PROBLEM);
  }

  private boolean isShowRetry() {
    if (payConfig != null
        && payConfig.payRetries != null
        && payConfig.payRetries.size() > 0
        && !TextUtils.isEmpty(payResult.payChannel)) {
      return payConfig.payRetries.contains(payResult.payChannel);
    }
    return false;
  }

  @Override
  protected void initListener() {
    retryBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        payTask.getPayUiController().showRetryQueryPayOrder(getActivity(), payTask, payResult);
        dismiss();
      }
    });

    viewHolder.setViewClick("tv_back_btn",new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        payTask.handlePayCallBack(payResult);
        dismiss();
      }
    });

    problemTv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        updateProblem();
      }
    });
  }

  private void updateProblem() {
    problemTv.setTextColor(Color.parseColor(PayConstant.HYKB_COLOR_DARK));
    problemTv.setHighlightColor(Color.parseColor("#00000000"));
    final String contact = payConfig.contact;
    String content = String.format(
        PayConstant.HYKB_STRING_ORDER_PROBLEM_CONTACT_FORMAT, contact);
    SpannableString spannableInfo = SpanUtils.getClickableSpannableString(content,
            contact, Color.parseColor(PayConstant.HYKB_COLOR_BLUE), new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        ClipboardUtils.copy(getActivity(), contact);
        ToastUtil.showToast(getActivity(), HYKB_QQ_COPY_TIP);
      }
    });
    problemTv.setText(spannableInfo);
    problemTv.setMovementMethod(LinkMovementMethod.getInstance());
  }

}